package com.study.system.search.controller;

import com.study.system.entity.PageEntity;
import com.study.system.search.service.CourseIndexService;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

@RequestMapping("/search")
@RestController
public class CourseIndexController {

    @Resource
    private CourseIndexService courseIndexService;

    @GetMapping("/init")
    public ResponseEntity<String> initIndex(){
        try {
            courseIndexService.initCourseIndex();
            return ResponseEntity.ok("初始化成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("/courses")
    public ResponseEntity<PageEntity> searchCourse(@RequestBody Map<String,String> map){
        try {
            PageEntity pageEntity = courseIndexService.searchCourse(map);
            return ResponseEntity.ok(pageEntity);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
